package com.slt.android.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.Random;

/**
 * Created by dp on 2017/12/18.
 */

public class MusicBar extends View {

    private int mPerWdith = 50;
    private int mPerHeight;
    Paint p = new Paint();

    private int viewWidth;
    private int viewHeight;

    public MusicBar(Context context) {
        super(context);
        initPait();
    }


    public MusicBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPait();
    }

    public MusicBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPait();
    }

    private void initPait() {
        p.setColor(Color.RED);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0 ; i < 10 ; i ++){
            Random random=new Random();
            mPerHeight = 1 + random.nextInt(10);
            mPerHeight = mPerHeight*45 + 10;
            RectF rect = new RectF(i*mPerWdith, 500-mPerHeight, (i+1)*mPerWdith, 500);
            canvas.drawRect(rect , p);
        }
        postInvalidateDelayed(200);
    }

    LinearGradient mLiLinearGradient;
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        viewWidth = getWidth();
        viewHeight = getHeight();
        mLiLinearGradient = new LinearGradient(0 , 0 , viewWidth , viewHeight , Color.YELLOW , Color.BLUE , Shader.TileMode.CLAMP);
        p.setShader(mLiLinearGradient);
    }
}
